<?php

/**
 * @author      :   QuangLT
 * @name        :   Plun_Plugin_Auth
 * @version     :   1.0
 * @copyright   :   Plun-Asia
 */

class Plun_Config
{
    /**
     * Hold Zend_Config_Ini instances
     * @var Array
     */
    protected static $_instances = array();

    /**
     * Job client
     * @var Fpt_JobClient
     */
    private static $jobClient = null;

    /**
     * Avoid creation of class instance
     */
    protected function __construct()
    {}
    
    protected function __clone()
    {}
    
    /**
     * Get configuration
     * @param <string> $module
     * @param <string> $filename
     * @return <Zend_Config_Ini>
     */
    public static function get($filename)
    {
        if (!isset(self::$_instances[$filename])
                && file_exists($filePath = APPLICATION_PATH . DS . 'configs' . DS . $filename . '-' . APP_ENV . '.ini'))
        {

            self::$_instances[$filename] = new Zend_Config_Ini($filePath);
        }
    
        return self::$_instances[$filename];
    }

    /**
     * Get job client
     * @return <Fpt_JobClient>
     */
    public static function getJobClient()
    {
        //Get Ini Configuration
        if(is_null(self::$_instances) )
        {
            self::$_instances = self::get(APP_CONFIG);
        }

        //Get caching instance
        if(is_null(self::$jobClient))
        {
            self::$jobClient =  Fpt_JobClient::getInstance(self::$_instances[APP_CONFIG]->job->toArray());
        }

        //Return jobclient
        return self::$jobClient;
    }

    /**
     * Get job function
     * @param <string> $name
     * @return <string>
     */
    public static function getJobFunction($name)
    {
        //Get Ini Configuration
        if(is_null(self::$_instances))
        {
            self::$_instances = self::get(APP_CONFIG);
        }

        //To array
        $jobConfiguration = self::$_instances[APP_CONFIG]->job->toArray();

        //Return job name
        return $jobConfiguration[$jobConfiguration['adapter']]['function'][$name];
    }
}